<template>
    <div class="dialog">
        <el-dialog :title="dialog.title" :visible.sync="dialog.show" :close-on-click-modal='false' :close-on-press-escape='false'
            :modal-append-to-body="false">
            <div class="form">
                <el-form ref="form" :model="formData" :rules="form_rules" label-width="120px" 
                    style="margin:10px;width:auto">
                    <el-form-item label="评论内容" prop="ComContent" style="height:150px">
                        <el-input type="textarea" v-model="formData.ComContent" :rows="7"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="评论时间" prop="Comtime">
                        <el-input type="content" v-model="formData.Comtime" placeholder="YYYY-MM-DD HH:mm:ss"></el-input>
                    </el-form-item> -->
                    <!-- <el-form-item label="用户id" prop="User_id">
                        <el-input type="content" v-model="formData.User_id"></el-input>
                    </el-form-item> -->
                    <!-- <el-form-item label="父评论id" prop="Father_id">
                        <el-input type="content" v-model="formData.Father_id"></el-input>
                    </el-form-item> -->
                    <el-form-item class="text_right">
                        <el-button @click="dialog.show=false">取消</el-button>
                        <el-button type="primary" @click="onSubmit('form')">提交</el-button>
                    </el-form-item>
                </el-form>

            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                form_rules: {
                    ComContent: [
                        { required: true, message: '评论内容不能为空', trigger: 'blur' }
                    ],
                    // Comtime: [
                    //     { required: true, message: '评论时间不能为空', trigger: 'blur' }
                    // ],
                    User_id: [
                        { required: true, message: '用户id不能为空', trigger: 'blur' }
                    ]
                }
            }
        },
        props: {
            dialog: Object,
            formData: Object
        },
        mounted(){
             
        },
        methods: {

            onSubmit(form) {

                this.$refs[form].validate(valid => {
                    if (valid) { 
                        const url=this.dialog.option=="add" ? "add" : "edit";
                         this.axios({
                             method: 'post',
                             url: `http://127.0.0.1:3000/api/postcomment/${url}`,
                            data: {
                                Cid:this.formData.Cid,
                                ComContent:this.formData.ComContent,
                                Comtime:this.formData.Comtime,
                                User_id:this.formData.User_id,
                                Father_id:this.formData.Father_id,
                            }
                        })
                            .then(res => {
                                this.$message({
                                    message: '成功',
                                    type: 'success',
                                })
                                this.dialog.show = false;
                                console.log(this.formData);
                                this.$router.go(0);
                            })
                            .catch(err =>{
                                console.log(err)
                            })

                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>